<template>
     <div class="baseEdit content_base">
      <h2>{{addOrChange}}</h2>
      <el-form ref='form' :model="editForm" style="width:90%" class='clearfix' label-width="250px" label-position="left" >
         <el-form-item label='圩堤名称：'  class="conWidth" >
               <el-input v-model="editForm.dikeName" placeholder="请输入圩堤名称，必填"></el-input>
           </el-form-item>
           <el-form-item label="所在设区市（含省直管县）:" class="conWidth" >
             <el-input v-model="editForm.city" placeholder="请输入所在设区市，必填"></el-input>
           </el-form-item>
           <el-form-item label="所在县（市，区）:"  class="conWidth">
             <el-input v-model="editForm.county" placeholder="请输入所县，必填"></el-input>
           </el-form-item></el-form>
       <div slot="footer" class="footer">
              <el-button type="primary" @click="addNewItemS()">{{addOrChangeSure}}</el-button>
      </div>
      </div>
</template>
<script>
export default {
  data(){
    return{
      editForm:{
        dikeName:'',
        city:'',
        county:'',
        dikeCode:''
      },
      addOrChange:'',
      addOrChangeSure:'',
      ac:1,//1为更改，2为新增

    }
  },
  methods:{
      
      addNewItemS(){
        //修改数据
        var _this = this
          if(this.ac==1){ 
              this.$axios.post('/dikeInfo/modifieddikeinfo',this.editForm).then(function(res){
                var mes = res.data.responseMessage;
                if(mes=='修改成功'){
                      _this.$message({ message: mes,
                                  type: 'success'});
                      _this.$router.push({name:'jectbase'})
                   }else{
                     _this.$message({ message: mes});
                   }
                }).catch(function(err){
                alert(1)
              })
          }else{
            //新增数据
              if(this.editForm.city==''||this.editForm.county==''||this.editForm.dikeName==''){
                  this.$message({ message:'请填写完整数据',
                                    type: 'error'});
                }else{
                  //去除填写过程中的空字符串
                  this.editForm.city =  this.editForm.city.replace(/\s*/g,"")
                  this.editForm.county =  this.editForm.county.replace(/\s*/g,"")
                  this.editForm.dikeName =  this.editForm.dikeName.replace(/\s*/g,"")
                  this.$axios.post('/dikeInfo/entrydikeinfo',this.editForm).then(function(res){
                      var mes = res.data.responseMessage
                        if(mes=='录入成功'){
                          _this.$message({ message: mes,
                                  type: 'success'});
                           _this.editForm.city = ''
                           _this.editForm.county = ''
                           _this.editForm.dikeName = ''
                        }else{
                           _this.$message({ message: mes});
                        }
                    }).catch(function(err){
                      console.log(err)
                    })
                }
          }
      },
    
      
  },
  mounted(){
    this.addOrChange = this.$route.params.addNew=='add'?'新增圩堤':'更改圩堤信息'
    this.addOrChangeSure = this.$route.params.addNew=='add'?'新增':'确认修改'
    this.ac = this.$route.params.addNew=='add'?2:1
    var dikeCode = this.$route.params.dikeCode;
    this.editForm.dikeCode = dikeCode
    var _this = this
    if(this.ac==1){
         //发送请求获得初始数据
         var thisDate = (new Date()).getTime()
        //  this.seleform.thisDate =  thisDate
       this.$axios.get(`/dikeInfo/getdikeinfobycode/${dikeCode}`,{
         params:{
           thisDate:thisDate
         }
       }).then(function(res){
         var codeMes = res.data.responseCode
         if(codeMes=='101'){
                _this.$router.push({name:'login'})
           }else{
             var data  = res.data.responseData
           _this.editForm = data
           }
       }).catch(function(err){
         console.log(err)
       })
    }
  }
}
</script>

<style scoped>
h2{
  height: 100px;
  line-height:100px;
}
.el-form{
  margin: 0 auto;
}
.el-form-item{
  width:500px;
  margin-left:20%;
  position: relative;
}
.el-form-item__content .el-input,
.el-form-item__content .el-select,
.el-form-item__content .el-textarea{
  width:200px;
  float: right;
  }
.footer{
    height: 300px;
    line-height: 300px;
    position: relative;
    width:90%;
    margin:0 auto;
  }
  .clearfix:after{
    content:'';
    clear: both;
    height: 0;
    display: block;
  }
   .el-button{
     width: 100px;
   }   
</style>
